package com.zeei.ems.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zeei.ems.base.dao.AreaCodeDao;
import com.zeei.ems.base.service.AreaCodeService;
import com.zeei.ems.base.tree.TreeNodeVO;
import com.zeei.ems.base.util.StringUtil;
import com.zeei.ems.base.util.TreeUtil;
import com.zeei.ems.base.vo.AreaCodeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service("areaCodeService")
public class AreaCodeServiceImpl implements AreaCodeService {
	@Autowired
	private AreaCodeDao areaCodeDao;

	/**
	 * @see com.zeei.bfs.service.AreaCodeService#queryAreaCode(com.zeei.ems.base.vo.AreaCodeVO)
	 */
	/** 查詢區域配置信息 */
	@Override
	public List<TreeNodeVO<AreaCodeVO>> queryAreaCodeTreeList(Map<String, Object> condition) {
		List<AreaCodeVO> list = areaCodeDao.queryAreaCodeList(condition);
		String regionName = StringUtil.valueOf(condition.get("regionName"), "");
		String regionCode = StringUtil.valueOf(condition.get("regionCode"), "");
		String userId = StringUtil.valueOf(condition.get("userId"), "");

		List<TreeNodeVO<AreaCodeVO>> resultTree = null;
		/*
		 * if (!CollectionUtils.isNotEmpty(list)) { return resultTree;// 失败 }
		 */
		if (StringUtils.isNotBlank(regionName) || StringUtils.isNotBlank(regionCode)
				|| StringUtils.isNotBlank(userId)) {
			List<AreaCodeVO> allList = areaCodeDao.queryAreaCodeList(new HashMap<String, Object>());
			List<TreeNodeVO<AreaCodeVO>> treeList = TreeUtil.buildSearchNodeTree(allList, list);
			resultTree = treeList;
		} else {
			resultTree = TreeUtil.buildTreeNodeTree(list, "");
		}
		return resultTree;
	}
	


	/**
	 * @see com.zeei.bfs.service.AreaCodeService#queryAreaCode(com.zeei.bfs.common.vo.bfs.AreaCodeVO)
	 */
	/** 查詢區域配置信息 */
	@Override
	public List<TreeNodeVO<AreaCodeVO>> queryAreaCodeTreeChildList(String regionCode, String filterFlag) {
		Map<String, Object> condition = new HashMap<>();

		List<AreaCodeVO> list = areaCodeDao.queryAreaCodeList(condition);

		List<TreeNodeVO<AreaCodeVO>> resultTree = null;

		resultTree = TreeUtil.buildTreeNodeTree(list, regionCode);

		return resultTree;
	}

	/**
	 * @see com.zeei.bfs.service.AreaCodeService#insertAreaCode(com.zeei.bfs.common.vo.bfs.AreaCodeVO)
	 */
	/** 插入区域信息 */
	@Override
	public boolean insertAreaCode(AreaCodeVO AreaCodeVO) {
		Integer conut = insetArea(AreaCodeVO);
		return conut > 0;
	}

	public Integer insetArea(AreaCodeVO AreaCodeVO){
		return areaCodeDao.insertAreaCode(AreaCodeVO);
	}

	/**
	 * @see com.zeei.bfs.service.AreaCodeService#updateAreaCode(com.zeei.bfs.common.vo.bfs.AreaCodeVO)
	 */
	/** 更新 */
	@Override

	public boolean updateAreaCode(AreaCodeVO areaCodeBena) {
		Integer conut = updateAreaAop(areaCodeBena);
		return conut > 0;
	}


	public Integer updateAreaAop(AreaCodeVO areaCodeBena){
		return areaCodeDao.updateAreaCode(areaCodeBena);
	}

	/**
	 * @see com.zeei.bfs.service.AreaCodeService#deleteAreaCode(List)
	 */
	/** 刪除 */
	@Override

	public boolean deleteAreaCode(List<String> list) {
		Integer conut = deleteAreaAop(list);
		return conut > 0;
	}


	public Integer deleteAreaAop(List<String> list){
		return areaCodeDao.deleteAreaCode(list);
	}

	@Override
	public List<AreaCodeVO> queryAreaInfor(String regionCode, Integer level) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("regionCode", regionCode);
		map.put("level", level);
		return areaCodeDao.queryAreaInfor(map);
	}

	@Override
	public List<AreaCodeVO> queryAreaCodeList(Map<String, Object> condition) {
		List<AreaCodeVO> list = areaCodeDao.queryAreaCodeList(condition);
		return list;
	}



	@Override
	public List<AreaCodeVO> queryAreaList() {
		return areaCodeDao.queryAreaList();
	}
	
	
}
